package com.zhang.search.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticsearchConfig {
    @Value("${myConfig.elasticSearch.host}")
    private String host;
    /*此处可以对其进行配置，这里暂时采用默认配置*/

    public static final RequestOptions COMMON_OPTIONS;
    static {
        RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
        COMMON_OPTIONS=builder.build();
    }
    @Bean
    public RestHighLevelClient esRestClient() {
        RestClientBuilder builder=null;
        //                    new HttpHost("localhost",9200,"http")可以配置多个（ES集群）
        builder=RestClient.builder(new HttpHost(host, 9200, "http"));
        return new RestHighLevelClient(builder);
    }
}
